//
//  provastringanome.c
//  
//
//  Created by Alessandro Bevilacqua on 31/05/12.
//  Copyright 2012 __MyCompanyName__. All rights reserved.
//

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#include <locale.h>
#include <signal.h>
#include <fcntl.h>

int roundit(double roundme) {
    double decimalpart = (double)(roundme - (int) roundme);
    if (decimalpart < 0.5) 
        return (int) roundme;
    else
        return ((int) roundme + 1);
}

int withinrange(int value, double oldmin, double oldmax, double newmin, double newmax) {
    double newvalue = (((value - oldmin) * (newmax - newmin)) / (oldmax - oldmin)) + newmin;
    return roundit(newvalue);
}

int main() {
    int randomdata = open("/dev/random", O_RDONLY);
    int myint=0,i;
    for(i=0; i<256; i++) {
        /*read(randomdata, &myint, 1);
        printf("%d \t--> ", myint);
        myint = withinrange(myint, (double) 0, (double) 255, (double) 0, (double) 20);*/
        printf("%d \t--> %d\n", i, withinrange(i, (double) 0, (double) 255, (double) 0, (double) 20));
        //printf("%d\n", myint);
    }
}
